package com.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.R;
import com.example.system.entity.Notice;
import com.example.system.mapper.NoticeMapper;
import com.example.system.service.INoticeService;
import com.example.system.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王鹤蒲
 * @since 2023-09-04
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    private Logger logger= LoggerFactory.getLogger(NoticeServiceImpl.class);

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private IUserService iUserService;

    @Override
    public R saveNotice(Notice notice) {
        LocalDateTime pubdate=LocalDateTime.now();
        notice.setPublisher(iUserService.getCurrentLoginUser().getUserName()).setPubdate(pubdate);
        noticeMapper.insert(notice);
        return R.success();
    }

    @Override
    public R updateNotice(Notice notice) {
        LocalDateTime updateTime=LocalDateTime.now();
        LambdaUpdateWrapper<Notice> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Notice::getId,notice.getId());
        lambdaUpdateWrapper.set(Notice::getUpdateBy,iUserService.getCurrentLoginUser().getUserName());
        lambdaUpdateWrapper.set(Notice::getUpdateTime,updateTime);
        lambdaUpdateWrapper.set(Notice::getTitle,notice.getTitle());
        lambdaUpdateWrapper.set(Notice::getCover,notice.getCover());
        lambdaUpdateWrapper.set(Notice::getContent,notice.getContent());
        lambdaUpdateWrapper.set(Notice::getStatus,notice.getStatus());
        lambdaUpdateWrapper.set(Notice::getType,notice.getType());
        noticeMapper.update(new Notice(),lambdaUpdateWrapper);
        return R.success();
    }

    @Override
    public R delete(Integer id) {
        LocalDateTime deleteTime=LocalDateTime.now();
        LambdaUpdateWrapper<Notice> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Notice::getId,id);
        lambdaUpdateWrapper.set(Notice::getDelFlag,"1");
        lambdaUpdateWrapper.set(Notice::getUpdateBy,iUserService.getCurrentLoginUser().getUserName());
        lambdaUpdateWrapper.set(Notice::getUpdateTime,deleteTime);
        noticeMapper.update(new Notice(),lambdaUpdateWrapper);
        return R.success();
    }

    @Override
    public R deleteBatch(Integer[] ids) {
        LocalDateTime deleteTime=LocalDateTime.now();
        LambdaUpdateWrapper<Notice> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        for(Integer id:ids){
            lambdaUpdateWrapper.clear();
            lambdaUpdateWrapper.eq(Notice::getId,id);
            lambdaUpdateWrapper.set(Notice::getDelFlag,"1");
            lambdaUpdateWrapper.set(Notice::getUpdateBy,iUserService.getCurrentLoginUser().getUserName());
            lambdaUpdateWrapper.set(Notice::getUpdateTime,deleteTime);
            noticeMapper.update(new Notice(),lambdaUpdateWrapper);
        }
        return R.success();
    }

    @Override
    public R findOne(Integer id) {
       LambdaQueryWrapper<Notice> lambdaQueryWrapper=new LambdaQueryWrapper<>();
       lambdaQueryWrapper.eq(Notice::getDelFlag,"0");
       lambdaQueryWrapper.eq(Notice::getId,id);
       return R.success(noticeMapper.selectOne(lambdaQueryWrapper));
    }

    @Override
    public R findAll() {
        LambdaQueryWrapper<Notice> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Notice::getDelFlag,"0");
        return R.success(noticeMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public R findAllByType(Integer type) {
        LambdaQueryWrapper<Notice> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Notice::getDelFlag,"0");
        lambdaQueryWrapper.eq(Notice::getStatus,'1');
        lambdaQueryWrapper.eq(Notice::getType,type);
        return R.success(noticeMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public R findPage(String title, int currentPage, int pageSize) {
        Page<Notice> page=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Notice> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StrUtil.isNotBlank(title),Notice::getTitle,title);
        lambdaQueryWrapper.eq(Notice::getDelFlag,"0");
        return R.success(noticeMapper.selectPage(page,lambdaQueryWrapper));
    }

    @Override
    public R importNotice(MultipartFile file) {
        return null;
    }

    @Override
    public void export(HttpServletResponse httpServletResponse) {

    }
}
